
--[[
    Event           Func
    OnDamageDealt   OnDamageDealt({damageTable = damageTable})
]]


if GameEventPublisher == nil then
    GameEventPublisher = {}
    GameEventPublisher._Events = {}
    GameEventPublisher._Event_Map = {
        OnDamageDealt = "OnDamageDealt"
    }
end

function GameEventPublisher.SubscribeGameEvent( event_name, subscriber)
    GameEventPublisher._Events[event_name] = GameEventPublisher._Events[event_name] or {}
    local entindex = subscriber:entindex()
    GameEventPublisher._Events[event_name][entindex] = true
end

function GameEventPublisher.UnsubscribeGameEvent( event_name, subscriber)
    GameEventPublisher._Events[event_name] = GameEventPublisher._Events[event_name] or {}
    local entindex = subscriber:entindex()
    GameEventPublisher._Events[event_name][entindex] = nil
end


function GameEventPublisher.Notify_OnDamageDealt(damageTable)
    local event_name = "OnDamageDealt"
    GameEventPublisher._NotifySubscriber( event_name, function ( subscriber )
        subscriber:OnDamageDealt(damageTable)
    end)
    -- GameEventPublisher._NotifySubscriber( event_name, {damageTable = damageTable}, function ( subscriber, args )
    -- end)
    -- GameEventPublisher._Events[event_name] = GameEventPublisher._Events[event_name] or {}
    -- for entindex,v in pairs(GameEventPublisher._Events[event_name]) do
    --     if v then
    --         local subscriber = EntIndexToHScript(entindex)
    --         if subscriber and IsValidEntity(subscriber) then
    --             local event_fun = GameEventPublisher._Event_Map[event_name]
    --             if event_fun then
    --                 if subscriber[event_fun] then
    --                     subscriber:OnDamageDealt(damageTable)
    --                 end
    --             end
    --         end
    --     end
    -- end
end

function GameEventPublisher._NotifySubscriber( event_name, handle_func)
    GameEventPublisher._Events[event_name] = GameEventPublisher._Events[event_name] or {}
    for entindex,v in pairs(GameEventPublisher._Events[event_name]) do
        if v then
            local subscriber = EntIndexToHScript(entindex)
            if subscriber and IsValidEntity(subscriber) then
                local event_fun = GameEventPublisher._Event_Map[event_name]
                if event_fun then
                    if subscriber[event_fun] then
                        handle_func(subscriber)
                    end
                end
            end
        end
    end
end

function GameEventPublisher._RunFunc( event_name, subscriber, args )
    -- local func = subscriber[event_fun]
    --                     func(args)
    -- PrintTable(args)
    if event_name == "OnDamageDealt" then
    end
end


GameRules.GameEventPublisher = GameEventPublisher
